/*
 *    Copyright (c) 2018-2025, boss All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the boss developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: boss
 */
package com.hopes.bizz.poll.factor.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hopes.bizz.common.core.util.CollectionUtils;
import com.hopes.bizz.common.core.util.StringUtils;
import com.hopes.bizz.poll.factor.entity.PollDisplayFactorEntity;
import com.hopes.bizz.poll.factor.entity.PollFactorInfoEntity;
import com.hopes.bizz.poll.factor.mapper.PollDisplayFactorMapper;
import com.hopes.bizz.poll.factor.service.PollDisplayFactorService;
import com.hopes.bizz.poll.factor.service.PollFactorInfoService;
import com.hopes.bizz.poll.monitor.service.PollMonitorFactorService;
import com.hopes.bizz.poll.monitor.service.PollMonitorTypeService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 数据展示因子
 *
 * @author ljh
 * @date 2023-05-22 10:00:56
 */
@Service
@RequiredArgsConstructor
public class PollDisplayFactorServiceImpl extends ServiceImpl<PollDisplayFactorMapper, PollDisplayFactorEntity> implements PollDisplayFactorService {

    private final PollFactorInfoService pollFactorInfoService;
    private final PollMonitorFactorService pollMonitorFactorService;
    private final PollMonitorTypeService pollMonitorTypeService;

    @Override
    public List<PollFactorInfoEntity> getByMonitorType(String monitorType) {
        LambdaQueryWrapper<PollDisplayFactorEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(PollDisplayFactorEntity::getMonitorType, monitorType);
        PollDisplayFactorEntity pollDisplayFactorEntity = Optional.ofNullable(baseMapper.selectOne(wrapper)).orElse(new PollDisplayFactorEntity());
        String ids = pollDisplayFactorEntity.getFactorIds();
        List<String> factorIds = null;
        // 是否有折算值由站点类型来确认
        LambdaQueryWrapper<PollFactorInfoEntity> pollFactorInfoWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotEmpty(ids)) {
            // 展示因子id
            factorIds = CollectionUtils.arrayToList(ids.split(","));
            // 查询因子
            pollFactorInfoWrapper
//                    .select(
//                            PollFactorInfoEntity::getId,
//                            PollFactorInfoEntity::getCode,
//                            PollFactorInfoEntity::getUnit)
                    .in(CollectionUtils.isNotEmpty(factorIds),PollFactorInfoEntity::getId,factorIds)
                    .eq(PollFactorInfoEntity::getUseFlag, true)
                    .orderByAsc(PollFactorInfoEntity::getSortNum);
            return pollFactorInfoService.list(pollFactorInfoWrapper).stream().
                    collect(Collectors.collectingAndThen(Collectors.toCollection(() ->
                            new TreeSet<>(Comparator.comparing(PollFactorInfoEntity :: getId))), ArrayList::new));
        }
        return Collections.emptyList();
    }

    @Override
    public List<PollFactorInfoEntity> getDisplayFactorByMonitorType(String monitorType) {
        final LambdaQueryWrapper<PollDisplayFactorEntity> wrapper = Wrappers.
                <PollDisplayFactorEntity>lambdaQuery().eq(PollDisplayFactorEntity::getMonitorType, monitorType);
        final PollDisplayFactorEntity pollDisplayFactorEntity = this.baseMapper.selectOne(wrapper);
        if (null == pollDisplayFactorEntity || StringUtils.isBlank(pollDisplayFactorEntity.getFactorIds())) {
            return null;
        }
        final String[] factorInfoArray = pollDisplayFactorEntity.getFactorIds().split(",");
        final LambdaQueryWrapper<PollFactorInfoEntity> queryWrapper = Wrappers.
                <PollFactorInfoEntity>lambdaQuery().in(PollFactorInfoEntity::getId, factorInfoArray);
        return pollFactorInfoService.list(queryWrapper);
    }

    @Override
    public List<PollFactorInfoEntity> getByMonitorTypeMain(Integer monitorTypeMain) {
        List<Integer> monitorTypeByMain = pollMonitorTypeService.getMonitorTypeByMain(monitorTypeMain);

        LambdaQueryWrapper<PollDisplayFactorEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(PollDisplayFactorEntity::getMonitorType, monitorTypeByMain);
        String ids = this.getBaseMapper().selectList(wrapper).stream().map(PollDisplayFactorEntity::getFactorIds).collect(Collectors.joining(","));
        List<String> factorIds;
        // 是否有折算值由站点类型来确认
        LambdaQueryWrapper<PollFactorInfoEntity> pollMonitorFactorWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotEmpty(ids)) {
            // 展示因子id
            factorIds = CollectionUtils.arrayToList(ids.split(","));
            // 查询因子
            pollMonitorFactorWrapper
//                    .select(
//                            PollFactorInfoEntity::getId,
//                            PollFactorInfoEntity::getCode,
//                            PollFactorInfoEntity::getUnit)
                    .in(CollectionUtils.isNotEmpty(factorIds), PollFactorInfoEntity::getId, factorIds)
                    .eq(PollFactorInfoEntity::getUseFlag, true)
                    .orderByAsc(PollFactorInfoEntity::getSortNum);
            List<PollFactorInfoEntity> list = pollFactorInfoService.list(pollMonitorFactorWrapper);

            return list.stream().
                    collect(Collectors.collectingAndThen(Collectors.toCollection(() ->
                            new TreeSet<>(Comparator.comparing(PollFactorInfoEntity::getId))), ArrayList::new));
        }
        return Collections.emptyList();
    }
}
